package com.zmn.plat.admin.controller.push;

import java.util.List;

import javax.annotation.Resource;

import com.google.common.collect.Lists;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dictionary.DictModel;
import com.zmn.common.dictionary.GlobalDict;
import com.zmn.common.dto.ResultDTO;
import com.zmn.common.utils.logger.LoggerUtil;
import com.zmn.plat.admin.controller.PlatBaseController;
import com.zmn.plat.business.interfaces.pic.PicUploadBService;
import com.zmn.plat.business.interfaces.push.PushNoticeAppBService;
import com.zmn.plat.common.constant.CompanyConsts;
import com.zmn.plat.model.vo.mast.ImageReslutDTO;
import com.zmn.plat.services.interfaces.push.notice.PushNoticeAppService;
import com.zmn.plat.services.interfaces.standard.record.StandardRecordService;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

/**
 * 描述: app通知消息基础控制类
 *
 * @author wangjie
 * @since 2020/3/24
 **/
public class PushNoticeAppBaseController extends PlatBaseController {

    protected static final String TAG = "app通知消息控制器";

    @Resource
    protected PushNoticeAppService pushNoticeAppService;

    @Resource
    protected PushNoticeAppBService pushNoticeAppBussService;

    @Resource
    protected StandardRecordService standardRecordService;

    @Resource
    protected PicUploadBService picUploadBService;

    /**
     * 描述: 消息推送规定的服务商字典
     *
     * @return java.util.List<com.zmn.common.dictionary.DictModel>
     * @author wangjie
     * @since 2020/4/23
     **/
    protected List<DictModel> listCompanyType() {
        List<DictModel> companyTypeItem = Lists.newArrayListWithExpectedSize(10);

        companyTypeItem.add(new DictModel(CompanyConsts.COMPANY_TYPE_SUBSIDIARY, "子公司"));
        companyTypeItem.add(new DictModel(CompanyConsts.COMPANY_TYPE_SERVICE, "服务商"));
        return companyTypeItem;
    }

    /**
     * 描述: 文件上传 （图片上传 为master模块提供文件上传）
     *
     * @param file
     *            文件对象
     * @param directory
     *            文件地址
     * @param elemId
     *            元素id
     * @param timestamp
     *            师傅id
     * @return com.zmn.common.dto.ImageUploadDTO
     * @author wangjie
     * @since 2018/10/22 10:56
     */
    @SuppressWarnings("Duplicates")
    protected ImageReslutDTO uploaderImage(MultipartFile file, String directory, String elemId, String timestamp) {
        ImageReslutDTO imageReslutDTO = new ImageReslutDTO();
        ImageReslutDTO.Data data = new ImageReslutDTO.Data();
        try {
            directory += timestamp + "/";
            String name = ((CommonsMultipartFile)file).getFileItem().getName();
            int index = name.lastIndexOf(".");
            if (index == -1) {
                return null;
            }
            String suffix = name.substring(index);
            StringBuilder fileName = new StringBuilder(elemId).append(suffix);

            ResultDTO resultDTO = picUploadBService.uploadPic(directory, fileName.toString(), file.getInputStream());
            if (resultDTO.getStatusBool()) {
                imageReslutDTO.setCode(GlobalDict.RESULT_SUCCESS);
                imageReslutDTO.setMsg(GlobalConsts.SUCCESS_MSG);
                data.setSrc(picUploadBService.getPicUrl(directory, fileName.toString()));
                imageReslutDTO.setData(data);
            }
            return imageReslutDTO;
        } catch (Exception e) {
            LoggerUtil.error(this.getClass(), e.getMessage(), e);
            return new ImageReslutDTO(GlobalDict.RESULT_FAIL, "文件上传失败", null);
        }
    }

}
